// pages/Manufactor/Manufactor.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cont:"",
    manufacturerList: [],
    manufactorName: "",
    searchmanufacturerList:[],
    isshowTow: false,
    isshow: true,
  },
  // 搜索
  onSearch(event) {

    this.setData({
      manufactorName: event.detail // 获取输入框内容
    });
    const searchResult = this.data.manufacturerList.filter(item =>
      item.name.includes(this.data.manufactorName)
    );
    if (searchResult.length == 0) {
      this.setData({
        searchmanufacturerList: []
      })
    } else {
      this.setData({
        searchmanufacturerList: searchResult, // 更新搜索结果
        isshow: false,
        isshowTow: true
      });
    }
  },
  // 取消搜索
  onCancel() {
    this.setData({
      searchmanufacturerList: [], // 更新搜索结果
      isshow: true,
      isshowTow: false,
      manufactorName:''
    });
  },
  goAddManufactor() {
    wx.navigateTo({
      url: '/pages/AddManufactor/AddManufactor',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log("进入厂家list页面")
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: "02"
      })
    }

    this.fetchManufacturerList();
  },
  // 跳转
  goUserOrderPage(event) {
    // console.log(event.currentTarget.dataset.item)
    const items = event.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/ManufactorOrder2/ManufactorOrder2?item=${items.name}`,
    })
  },
  // 请求厂家
  fetchManufacturerList: function () {
    wx.cloud.callFunction({
      name: 'getManufactorList',
      success: res => {
        if (res.result.success) {
          this.setData({
            manufacturerList: res.result.data,
            cont:res.result.data.length
          });
          console.log("this.data.manufacturerList", this.data.manufacturerList)
        } else {
          wx.showToast({
            title: 'Failed to fetch data',
            icon: 'none'
          });
        }
      },
      fail: err => {
        console.error('Failed to call cloud function:', err);
        wx.showToast({
          title: 'Failed to fetch data',
          icon: 'none'
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.fetchManufacturerList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})